// pages/buyTicket/buyTicket.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tickets: [{
      count: 0,
      num: 19,
      sendTickets: []
    }, {
        count: 0,
        num: 29,
        sendTickets: []
    }, {
        count: 0,
        num: 39,
        sendTickets: []
    }],
    sendTickets: [0]
  },
  //删减优惠券
  removeCount:function(res){
    console.log(res.currentTarget.dataset.index)
    var index = res.currentTarget.dataset.index
    var ticket = "tickets[" + index + "].count"
    var preTicket = this.data.tickets[index].count
    var sendTickets = "tickets[" + index + "].sendTickets"
    if (preTicket==0){
      // 数量为0不能继续减
      this.setData({
        [ticket]: 0
      })
    }else{
      this.setData({
        [ticket]: preTicket -1
      })
    }
    if (this.data.tickets[index].count % 5 == 0) {
      var arr = []
      arr = this.data.tickets[index].sendTickets
      arr.push(0)
      this.setData({
        [sendTickets]: arr
      })
    }
  },
  //增加优惠券
  addCount:function(res){
 
    var index = res.currentTarget.dataset.index
    var ticket = "tickets["+index+"].count"
    var sendTickets = "tickets[" + index + "].sendTickets"
    var preTicket = this.data.tickets[ index].count
    
    this.setData({
      [ticket]: preTicket +1
    })
    if(this.data.tickets[index].count%5==0){
       var arr = []
      arr = this.data.tickets[index].sendTickets
     arr.push(0)
      this.setData({
        [sendTickets]: arr
      })
    }
  },
  // 用户手动输入优惠券数量
  inputTicketCount: function(res){
      var count = parseInt(res.detail.value)
      var index = this.data.currentIndex
      var ticket = "tickets[" + index + "].count"
      this.setData({
        [ticket]: count
      })
  },
  // 优惠券结算
  pay:function(){
    console.log(this.data.tickets)
  },
//用户手动输入获取当前item index
  inputClick:function(res){
   this.setData({
     currentIndex: res.currentTarget.dataset.index
   })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})